#pragma once
#include "..\\GameEngine\\GameEngine.h"

class SpaceBack : public Background
{
    TGDKSprite* black;
    list<TGDKSprite *> lstSpr;
public:
    SpaceBack()
    {
        srand(timeGetTime()%65535);
        black = new TGDKSprite(*ResourceManager::GetResource("Black"), NULL, 0, 0);
        black->SetPriority(0);

        TGDKImage& img = *ResourceManager::GetResource("Cloud");
        for(int i=0;i<25;i++)
        {
            TGDKSprite* spr = new TGDKSprite(img, NULL, 0, 0);
            GenerateNew(spr);
            lstSpr.push_back(spr);
        }
    }

    void GenerateNew(TGDKSprite* spr, BOOL bRecreate = FALSE)
    {
        spr->SetPriority(0);
        float angle;
        if (bRecreate)
        {            
            if (rand()%10 > 5)
            {
                spr->XPos = 640 + spr->GetWidth();
                angle = 270-(rand()%80-40);
            }else{
                spr->XPos = -spr->GetWidth();
                angle = 90 + (rand()%80-40);
                
            }
            
            spr->YPos = rand()%480;            
        }
        else
        {
            if (rand()%10 > 5)
            {
                spr->XPos = (640/2) + rand()%(640/2 + 400);
                angle = 270-(rand()%80-40);
            }else{
                spr->XPos = rand()%(640/2 - 400);
                angle = 90 + (rand()%80-40);

            }

            spr->YPos = rand()%480;            
        }
        
        spr->SetScale(rand()%50+70);
        spr->SetDiffuse(245,rand()%255,rand()%128);
        spr->SetAngle(angle);
        spr->SetAlpha(rand()%32+24);
        spr->Refresh();
    }
    virtual ~SpaceBack()
    {        
        delete black;
        while (!lstSpr.empty())
        {
            list<TGDKSprite *>::iterator it = lstSpr.begin();
            delete (*it);
            lstSpr.remove(*it);
        }        
    }
    virtual void DoAction() 
    {
        list<TGDKSprite *>::iterator it = lstSpr.begin();
        for(; it != lstSpr.end();it++)
        {
            TGDKSprite *spr = *it;
            spr->Move(0.85f);
            int x = spr->GetX();
            int w = spr->GetWidth();
            int y = spr->GetY();
            int h = spr->GetHeight();
            if (x>640+w || x<-w || y<-h || y>480+h)
            {
                GenerateNew(spr, TRUE);
            }
        }
    }

    virtual void OnDraw() 
    {
                
    }

};